package com.nondo.rdp.stock.service.impl;

import com.nondo.rdp.auth.domain.Role;
import com.nondo.rdp.auth.endpoint.model.Page;
import com.nondo.rdp.stock.domain.SimulateLog;
import com.nondo.rdp.stock.repository.SimulateLogRepository;
import com.nondo.rdp.stock.service.SimulateLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Transactional
@Service
public class SimulateLogServiceImpl implements SimulateLogService {
    @Autowired
    SimulateLogRepository simulateLogRepository;

    @Override
    public SimulateLog findOne(Long id) {
        return this.simulateLogRepository.findOne(id);
    }

    @Override
    public SimulateLog save(SimulateLog simulateLog) {
        return this.simulateLogRepository.save(simulateLog);
    }

    @Override
    public void update(SimulateLog simulateLog) {
        this.simulateLogRepository.save(simulateLog);
    }

    @Override
    public void delete(Long id) {
        this.simulateLogRepository.deleteById(id);
    }

    @Override
    public Page<SimulateLog> findAll(Page<SimulateLog> page, SimulateLog simulateLog) {
        Sort sort = new Sort(Sort.Direction.DESC, "createDate");
        PageRequest pageRequest = new PageRequest(page.getPageNumber(), page.getPageSize(), sort);
        org.springframework.data.domain.Page<SimulateLog> dictPage = simulateLogRepository.findAll((root, query, builder) -> {

            List<Predicate> predicates = new ArrayList<>();
            //搜索股票代码
            if (StringUtils.isNotEmpty(simulateLog.getCode())) {
                predicates.add(builder.like(root.get("code"), "%" + simulateLog.getCode() + "%"));
            }
            //搜索用户
            if (simulateLog.getUser() != null) {
                predicates.add(builder.equal(root.get("user"), simulateLog.getUser().getId()));
            }
            //搜索开始时间,结束时间
            if (simulateLog.getCreateTime() != null) {
                predicates.add(builder.greaterThan(root.get("createTime"), simulateLog.getCreateTime()));
            }
            if (simulateLog.getSearchEndTime() != null) {
                //处理时间
                predicates.add(builder.lessThan(root.get("createTime"), simulateLog.getSearchEndTime()));
            }

            predicates.add(builder.like(root.get("delFlag"), Role.DEL_FLAG_NORMAL));
            Predicate[] predicatesArray = new Predicate[predicates.size()];
            predicates.toArray(predicatesArray);
            return builder.and(predicatesArray);

        }, pageRequest);

        return new Page<SimulateLog>(dictPage);
    }

    @Override
    public List<SimulateLog> findAllByCodeAndCreateTimeAfter(String code, Date createTime) {
        return this.simulateLogRepository.findAllByCodeAndCreateTimeAfter(code, createTime);
    }
}